package com.xie.o2o.controller.frontend;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xie.o2o.controller.GlobalExceptionHandler;
import com.xie.o2o.entity.HeadLine;
import com.xie.o2o.entity.ShopCategory;
import com.xie.o2o.service.HeadLineService;
import com.xie.o2o.service.ShopCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @create: xie
 */
@Controller
@RequestMapping("/frontend")
public class MainPageController {
    private final static Logger log= LoggerFactory.getLogger(MainPageController.class);

    @Autowired
    private HeadLineService headLineService;
    @Autowired
    private ShopCategoryService shopCategoryService;


    @ResponseBody
    @RequestMapping(value = "/listmainpageinfo", method = RequestMethod.GET)
    public Map<String, Object> listMainPageInfo() {
        Map<String, Object> map = Maps.newHashMap();
        List<ShopCategory> shopCategoryList;
        List<HeadLine> headLineList;
        try {
            //获取一级店铺分类
            shopCategoryList = shopCategoryService.getShopCategoryList(null);
            map.put("shopCategoryList", shopCategoryList);
        } catch (Exception e) {
            log.error("获取一级分类失败:"+e.toString());
            throw new RuntimeException("获取分类失败");
        }

        try {
            //获取状态码为1的可用头条
            HeadLine headLine = new HeadLine();
            headLine.setEnableStatus(1);
            headLineList = headLineService.getHeadLineList(headLine);
            map.put("headLineList",headLineList);
        }catch (Exception e){
            log.error("获取头条失败:"+e.toString());
            throw new RuntimeException("获取头条失败");
        }
        map.put("success",true);
        return map;
    }
}
